package org.example.hw.model;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.envers.Audited;
import org.hibernate.validator.constraints.Email;

@Entity
@Table(name = "HW_EMAILS_ALLOWED")
@Audited
// TODO: naći neko bolje ime za klasu
public class EmailAllowed extends AbstractEntity {

	@Email
	// TODO: @NotBlank?
	private String email;

	@NotNull
	@ManyToOne
	@JoinColumn(nullable = false)
	private Link link;

	public EmailAllowed() {

	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().
				appendSuper(super.hashCode()).
				append(email).
				hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}

		final EmailAllowed otherEmailerAllowed = (EmailAllowed) obj;

		return new EqualsBuilder().
				appendSuper(super.equals(obj)).
				append(email, otherEmailerAllowed.getEmail()).
				isEquals();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
				append("email", email).
				append("creationTime", getCreationTime()).
				append("modificationTime", getModificationTime()).
				toString();
	}

}
